package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.PageResult;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.YddUser;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.exception.CustomException;
import com.itheima.youdianda.service.YddUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/ydd_user")
public class YddUserController {
    @Autowired
    private YddUserService yddUserService;

    @Value("${YouDianDa.salt}")
    private String salt;

    /**
     * 根据id查询会员信息
     */
    @GetMapping("/rowInfo")
    public R findById(String id){
        YddUser yddUser = yddUserService.getById(id);
        return R.success(yddUser,"查询成功!");
    }

    /**
     * 会员列表
     */
    @GetMapping("/list")
    public R getList(@RequestParam Map map){
        //接受前端传的数据
        String sex = map.get("sex").toString();
        String username = map.get("username").toString();
        String mobile = map.get("mobile").toString();
        String page = map.get("page").toString();
        String limit = map.get("limit").toString();

        //构造分页构造器
        Page<YddUser> userPage = new Page<>(Integer.parseInt(page),Integer.parseInt(limit));
        //构造条件构造器
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(sex !="",YddUser::getSex,sex);
        queryWrapper.eq(username !="",YddUser::getUsername,username);
        queryWrapper.eq(mobile !="",YddUser::getMobile,mobile);
        queryWrapper.orderByAsc(YddUser::getId);

        //执行查询
        Page<YddUser> pageInfo = yddUserService.page(userPage, queryWrapper);
        PageResult pageResult = new PageResult();
        pageResult.setCount(pageInfo.getTotal());
        pageResult.setTotalPages((long) Math.ceil((double) pageInfo.getTotal() / pageInfo.getSize()));
        pageResult.setPageSize(pageInfo.getSize());
        pageResult.setCurrentPage(pageInfo.getCurrent());

        PageDto<YddUser> pageDto = new PageDto<>();
        BeanUtils.copyProperties(pageInfo, pageDto);
        pageDto.setValue(pageInfo);

        return R.success(pageDto);

    }

    /**
     *添加会员
     */
    @PostMapping("/add")
    public R add(@RequestBody YddUser yddUser){
        if(yddUser.getPassword()!=null){
            yddUser.setPassword(DigestUtils.md5DigestAsHex((yddUser.getPassword()+salt).getBytes()));
        }
        yddUserService.save(yddUser);
        return R.success("添加成功!");
    }

    /**
     *删除会员
     */
    @GetMapping("/del")
     public R deleteById(String id){
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,YddUser::getId,id);
        YddUser one = yddUserService.getOne(queryWrapper);
        if(one.getDefaultData()!=null){
            return R.error("系统默认数据,不能删除");
        }
        yddUserService.removeById(id);
        return R.success("删除成功!");
     }

    /**
     * 修改会员信息
     */
    @PostMapping("/update")
    public R update(@RequestBody YddUser yddUser){
        if(yddUser.getPassword()!=null){
            yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes())+salt);
        }
        yddUserService.updateById(yddUser);
        return R.success("修改成功!");
     }
}
